/**
 * @ClassName: AppRegisterController
 * @Description
 * @Author maozhi
 * @Date 2021/3/22
 * @Time 14:59
 */

package com.universityJob.modules.app.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.universityJob.common.utils.R;
import com.universityJob.common.utils.UUIDUtil;
import com.universityJob.common.validator.ValidatorUtils;
import com.universityJob.modules.app.entity.UserEntity;
import com.universityJob.modules.app.form.RegisterForm;
import com.universityJob.modules.app.service.UserService;
import com.universityJob.modules.app.utils.WXPublicUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * 注册
 */
@RestController
@RequestMapping("/app")
@Api("APP注册接口")
public class AppRegisterController {
    @Autowired
    private UserService userService;
    @Autowired
    private WXPublicUtils wxPublicUtils;

    private Logger logger = LoggerFactory.getLogger(getClass());


    @PostMapping("register")
    @ApiOperation("注册")
    public R register(@RequestParam Map map) throws IOException {
        String userId = MapUtils.getString(map,"userId","");
        if (userId == null || userId.isEmpty() || "null".equals(userId)){

            return R.error("CODE不能为空！");
        }
        String openId = wxPublicUtils.getOpenId(userId);
        if (openId == null || openId.isEmpty()|| "".equals(openId) ){
            return R.error("获取openid失败");
        }
        logger.info("{} 的openid 为 {}",MapUtils.getString(map,"name",""),openId);

        UserEntity user = new UserEntity();
        user.setAll(openId, MapUtils.getString(map,"username",""), MapUtils.getString(map,"mobile",""),
                DigestUtils.sha256Hex(MapUtils.getString(map,"password","123")),
                MapUtils.getString(map,"gender","1"), MapUtils.getString(map,"email",""),
                MapUtils.getString(map,"source",""), MapUtils.getString(map,"major",""),
                MapUtils.getString(map,"name",""),
                "", MapUtils.getString(map,"status","1"), MapUtils.getString(map,"college",""),
                MapUtils.getString(map,"education","本科"),new Date());
        userService.save(user);
        return R.ok();
    }
}
